package color.koitq.picker.fragment

import android.annotation.SuppressLint
import android.graphics.Color
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import color.koitq.picker.R
import color.koitq.picker.ad.AdFragment
import color.koitq.picker.adapter.Tab2Adapter
import com.blankj.utilcode.util.ClipboardUtils
import com.blankj.utilcode.util.ResourceUtils
import com.blankj.utilcode.util.ToastUtils
import com.quexin.pickmedialib.MediaUtils
import com.skydoves.multicolorpicker.ColorEnvelope
import com.skydoves.multicolorpicker.listeners.ColorListener
import kotlinx.android.synthetic.main.fragment_tab2.*

class Tab2Frament : AdFragment() {
    private var mView: View? = null
    private var coldColor = arrayOf(
        "#8432FF",
        "#4531E1",
        "#324FFF",
        "#5490FF",
        "#32CCFF",
        "#33BCD5",
        "#38ECD0",
        "#65FF36",
        "#33B530"
    )
    private var warmColor = arrayOf(
        "#FF32A1",
        "#DC0862",
        "#FF3245",
        "#CE2B2B",
        "#FF6147",
        "#FF8D30",
        "#B96310",
        "#FFC936",
        "#DEFF2A"
    )

    override fun getLayoutId(): Int {
        return R.layout.fragment_tab2
    }

    override fun initKotlinWidget() {
        super.initKotlinWidget()
        topbar.setTitle("颜色")
        initColorPicer()
        initListCold()
        initListWarm()
        color.setOnClickListener {
            mView = it
            showVideoAd()
        }
        color1.setOnClickListener {
            mView = it
            showVideoAd()
        }
        color2.setOnClickListener {
            mView = it
            showVideoAd()
        }
    }

    private fun initListWarm() {
        color2.text = warmColor[0]
        color2.setTextColor(Color.parseColor(warmColor[0]))
        var mAdapter = Tab2Adapter()
        mAdapter.setNewInstance(warmColor.toMutableList())
        list2.layoutManager = LinearLayoutManager(mContext, RecyclerView.HORIZONTAL, false)
        list2.adapter = mAdapter
        mAdapter.setOnItemClickListener { adapter, view, position ->
            color2.text = mAdapter.getItem(position)
            color2.setTextColor(Color.parseColor(mAdapter.getItem(position)))
        }

    }

    private fun initListCold() {
        color1.text = coldColor[0]
        color2.setTextColor(Color.parseColor(coldColor[0]))
        var mAdapter = Tab2Adapter()
        mAdapter.setNewInstance(coldColor.toMutableList())
        list1.layoutManager = LinearLayoutManager(mContext, RecyclerView.HORIZONTAL, false)
        list1.adapter = mAdapter
        mAdapter.setOnItemClickListener { adapter, view, position ->
            color1.text = mAdapter.getItem(position)
            color1.setTextColor(Color.parseColor(mAdapter.getItem(position)))
        }
    }

    private fun initColorPicer() {
        multiColorPickerView.setPaletteDrawable(ResourceUtils.getDrawable(R.mipmap.tab2_color_circle))
        multiColorPickerView.addSelector(
            ResourceUtils.getDrawable(R.mipmap.wheel),
            object : ColorListener {
                @SuppressLint("SetTextI18n")
                override fun onColorSelected(envelope: ColorEnvelope) {
                    if (envelope.color == 0) return
                    color.text = "#${envelope.htmlCode}"
                    color.setTextColor(envelope.color)
                }
            })
    }

    override fun fragmentAdClose() {
        topbar.post {
            if (mView != null) {
                when (mView) {
                    color -> ClipboardUtils.copyText(color.text)
                    color1 -> ClipboardUtils.copyText(color1.text)
                    color2 -> ClipboardUtils.copyText(color2.text)
                }
                ToastUtils.showShort("已复制到剪贴板")
            }
        }
    }
}